package com.rzico.basics.amap;

import com.rzico.basics.amap.pojo.GaodeLocation;
import com.rzico.basics.amap.pojo.GeoPOIQuery;
import com.rzico.basics.amap.pojo.regeo.AddressComponent;
import com.rzico.basics.amap.pojo.request.GeoCodeRequest;
import com.rzico.basics.amap.pojo.request.POIRequest;
import com.rzico.basics.amap.pojo.response.IPLocationResponse;
import com.rzico.basics.amap.util.GaoDeMapUtil;
import com.rzico.basics.amap.util.GeoIPLocationUtil;
import com.rzico.basics.amap.util.GeoQueryUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lam on 2018/8/8.
 */
@Component
public class AmapUtils {

    @Value("${amap.key}")
    private String key;

    public Map<String,Object> geoCode(double lat, double lng) {

        GeoCodeRequest geoCodeRequest = new GeoCodeRequest();
        geoCodeRequest.setKey(key);
        geoCodeRequest.setLocation(lng + "," + lat);
        geoCodeRequest.setRadius("2000");
//        geoCodeRequest.setPoitype("120000");
        geoCodeRequest.setExtensions("all");
        GaoDeMapUtil gaoDeMapUtil = new GaoDeMapUtil();

        GaodeLocation gaodeLocation = gaoDeMapUtil.getReGeoodes(geoCodeRequest);
        if (gaodeLocation.getError()) {
            return null;
        }
        if (gaodeLocation.getStatus().equals("0")) {
            return null;
        } else {
            AddressComponent addr = gaodeLocation.getRegeocode().getAddressComponent();
            String adcode = addr.getAdcode();
            Map<String, Object> data = new HashMap<>();

            if (adcode!=null && adcode.length()==6){
                data.put("areaId", adcode);
            }
            data.put("name",addr.getBuilding().getName());
            data.put("city",addr.getCity());
            if (addr.getProvince().equals(addr.getCity())) {
                data.put("areaName",addr.getCity().concat(addr.getDistrict()));
                data.put("address", addr.getCity().concat(addr.getDistrict()).concat(addr.getStreetNumber().getStreet()).concat(addr.getStreetNumber().getNumber()));
            } else {
                data.put("areaName",addr.getProvince().concat(addr.getCity()).concat(addr.getDistrict()));
                data.put("address", addr.getProvince().concat(addr.getCity()).concat(addr.getDistrict()).concat(addr.getStreetNumber().getStreet()).concat(addr.getStreetNumber().getNumber()));
            }

            return data;
        }
    }

    public Map<String,Object> geoPois(double lat, double lng) {
        GeoCodeRequest geoCodeRequest = new GeoCodeRequest();
        geoCodeRequest.setKey(key);
        geoCodeRequest.setLocation(lng + "," + lat);
        geoCodeRequest.setRadius("2000");
        //geoCodeRequest.setPoitype("120000");
        geoCodeRequest.setExtensions("all");
        GaoDeMapUtil gaoDeMapUtil = new GaoDeMapUtil();

        GaodeLocation gaodeLocation = gaoDeMapUtil.getReGeoodes(geoCodeRequest);
        if (gaodeLocation.getError()) {
            return null;
        }
        if (gaodeLocation.getStatus().equals("0")) {
            return null;
        } else {
            Map<String, Object> data = new HashMap<>();

            data.put("pois", gaodeLocation.getRegeocode().getPois());

            return data;
        }
    }


    public Map<String,Object> geoPoisQuery(String keyword,Double lat,Double lng) {

        POIRequest geoPOIRequest = new POIRequest();
        geoPOIRequest.setKey(key);
        geoPOIRequest.setKeywords(keyword);
        //geoPOIRequest.setTypes("120000");
        GaoDeMapUtil gaoDeMapUtil = new GaoDeMapUtil();

        if (lat!=null && lng!=null) {
            Map<String,Object> data = geoCode(lat,lng);
            if (data.get("city")!=null) {
                geoPOIRequest.setCity(data.get("city").toString());
            }
        }

        GeoPOIQuery geoPOIQuery = GeoQueryUtil.keywordsPlace(geoPOIRequest);
        if (geoPOIQuery.getError()) {
            return null;
        }

            Map<String, Object> data = new HashMap<>();

            data.put("pois", geoPOIQuery.getPois());

            return data;

    }

    public Map<String,Object> IPLocation(String ip) {
        IPLocationResponse ipLocationResponse = GeoIPLocationUtil.searchGeoDistrict(key,ip);
        Map<String, Object> data = new HashMap<>();
        if (ipLocationResponse.getStatus().equals("0")) {
            data.put("areaId", "350200");
            data.put("name", "厦门市");
            data.put("address", "厦门市");
        } else {
            String adcode = ipLocationResponse.getAdcode();

            if (adcode!=null && adcode.length()==6){
                data.put("areaId", adcode);
                data.put("name", ipLocationResponse.getProvince().concat(ipLocationResponse.getCity()));
                data.put("address", ipLocationResponse.getProvince().concat(ipLocationResponse.getCity()));
            } else {
                data.put("areaId", "350200");
                data.put("name", "厦门市");
                data.put("address", "厦门市");
            }
        }


        return data;
    }
}
